package com.suneee.cloud.admin.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.cloud.admin.dao.AdministratorDao;
import com.suneee.cloud.admin.entity.AdministratorBean;
import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.core.util.DateUtils;
import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.sso.admin.entity.Administrator;

@Repository("administratorDao")
public class AdministratorDaoImpl extends GenericDao<Administrator, Integer> implements AdministratorDao {

	@Override
	public void updPassword(String adminId,String newPassword) {
		executeUpdate("update t_administrator set password = ?, last_update_time = ?  where admin_id = ?", newPassword, DateUtils.getCurrentTimestamp(), adminId);
	}

	@Override
	public Page<Map<String,Object>> list(AdministratorBean bean) 
	{
		String sql = "SELECT a.admin_id AS adminId,a.enterprise_code AS enterpriseCode,e.name as enterpriseName,a.dept_code AS deptCode,d.name AS deptName,a.account,a.password,a.name,a.nick,a.photo,a.sex,a.enabled,a.email,a.mobile,a.register_time AS registerTime,a.last_update_time AS lastUpdateTime,a.address,a.signature,a.background_img AS backgroundImg,f.name AS createPerson,a.admin FROM t_administrator a LEFT JOIN t_administrator f ON a.create_person = f.admin_id  LEFT JOIN t_department d ON a.dept_code = d.dept_code LEFT JOIN t_enterprise e ON e.enterprise_code = a.enterprise_code WHERE 1 = 1 ";
		WhereClauseBuilder builder = new WhereClauseBuilder(sql);
		if (bean == null) {
            bean = new AdministratorBean();
		}
        builder.equals("a.enterprise_code", bean.getEnterpriseCode());
        builder.contains("a.account", bean.getAccount());
        builder.contains("a.name", bean.getName());
        builder.contains("e.name", bean.getEnterpriseName());
        builder.contains("a.nick", bean.getNick());
        builder.equals("a.enabled", bean.getEnabled());
        builder.equals("a.email", bean.getEmail());
        builder.equals("a.mobile", bean.getMobile());
        builder.contains("a.address", bean.getAddress());
        builder.equals("a.signature", bean.getSignature());
        builder.equals("a.admin",bean.isAdmin());
        
        if(!StringUtils.isEmpty(bean.getDeptCode()))
		{
        	if(bean.getDeptCode().indexOf(",") != -1)
        	{
        		String [] deptArea = bean.getDeptCode().split(",");
        		builder.equals("a.dept_code",deptArea[deptArea.length-1]);
        	}
        	else
        	{
        		builder.equals("a.dept_code",bean.getDeptCode());
        	}
		}
        
        
        
        builder.between("a.register_time", bean.getStartRgisterTime(), bean.getEndRgisterTime());
        builder.between("a.last_update_time", bean.getStartLastUpdateTime(), bean.getEndLastUpdateTime());
        bean.setPageNumber(bean.getPageNumber() == 0 ? 1 : bean.getPageNumber());
        bean.setPageSize(bean.getPageSize() == 0 ? 10 : bean.getPageSize());
        bean.setSort((bean.getSort() == null || StringUtils.isEmpty(bean.getSort().getOrderByClause())) ? new Sort(
                "a.register_time", Order.DESC) : bean.getSort());
        return queryForList(builder.getSql(), bean, builder.getArgs());
	}

	
	
	@Override
	public boolean exists(String key, String value) {
		List<Administrator> list = this.selectWhere(key + " = ? ", value);
        if(list != null && list.size() > 0){
            return true;
        }
        return false;
	}

	@Override
	public boolean existsAccount(String account) {
		return this.exists("account", account);
	}

	@Override
	public boolean existsAdmin(String enterpriseCode) {
		WhereClauseBuilder builder = new WhereClauseBuilder();
	    builder.equals("enterprise_code", enterpriseCode);
	    builder.equals("admin", "1");//管理员标识
	    List<Administrator> adminList = this.selectWhere(builder.getSql(), builder.getArgs());
	    if(adminList!= null && adminList.size() >0){
	    	return true;
	    }else{
	    	return false;
	    }
	}

	@Override
	public boolean existsAccount(String enterpriseCode, String account) {
		WhereClauseBuilder builder = new WhereClauseBuilder();
	    builder.equals("enterprise_code", enterpriseCode);
	    builder.equals("account", account);
	    List<Administrator> adminList = this.selectWhere(builder.getSql(), builder.getArgs());
	    if(adminList!= null && adminList.size() >0){
	    	return true;
	    }else{
	    	return false;
	    }
	}

	@Override
	public boolean existsAccountAdmin(String account) {
		String sql ="select admin_id from t_administrator where account=? and enterprise_code='"+Administrator.CODE+"'";
//		String sql ="select admin_id from t_administrator where account=? ";
		List<Administrator> adminList = this.executeQuery(sql, account);
		if(adminList!= null && adminList.size() >0){
	    	return true;
	    }else{
	    	return false;
	    }
	}
	
	@Override
	public void deleteAccount(String enterpriseCode) {
		this.deleteWhere(" enterprise_code=?", enterpriseCode);
	}

	@Override
	public List<Administrator> getListByEnterpriseCode(String enterpriseCode) {
		// TODO Auto-generated method stub
		return this.selectWhere(" enterprise_code = ? ", enterpriseCode);
	}
}
